<?php
require_once 'Artdepot/Controller/Action.php';

class IndexController extends Artdepot_Controller_Action
{

    public function init()
    {
    	parent::init();
    }
    
    public function thanksAction(){
    	
    	
    }
    
	public function commentAction(){
    	$form = new Form_Comment();
    	if($this->_request->isPost()){
    		if($form->isValid($this->_request->getParams())){
    			$message = $form->getValues();
    			$this->sendCommentEmail($message);
    			$this->view->message = "感谢您的留言，我们会认真对待您的宝贵意见！";
    		}
    	}
    	$this->view->form = $form;
    	
    }

    public function indexAction()
    {
   		$cateTable = new Model_DbTable_ArtworkCategory();
		$cates = $cateTable->fetchAll();
		$categories = array();
		$categoriesMap = array();
		$subIds = array();
		foreach ($cates as $cate) {
			if ($cate['parent']==0) {
				$categories[$cate['id']]['self'] = $cate; 
			} else {
				$categoriesMap[$cate['id']] = $cate->name;
				$categories[$cate['parent']]['childs'][] = $cate;
				if ($cateId && $cate['parent']==$cateId) {
					$subIds[] = $cate->id;
				}
			}
		}
		
		$this->view->categories = $categories;
    	
		$artworkTable = new Model_DbTable_ArtWork();
    	
		//跑马灯
    	$this->view->paomadengs = $artworkTable->fetchAll("typical = 'Y'");
		
		$categoryTable = new Model_DbTable_ArtworkCategory();
		$cates = $categoryTable->fetchAll();
		$cateMap = array();
		foreach ($cates as $cate) {
			$cateMap[$cate->id] = $cate->name;
		}
		$artworks = $artworkTable->fetchAll("display_positions=1", array('display_order', 'id'), 18);
		
		//首页推荐新闻 1个
		$newsTable = new Model_DbTable_News();
		$recommandNews = $newsTable->fetchRow('id=1');
		$this->view->compIntro = $recommandNews;
		
		
		$this->view->categoryMap = $cateMap;
		$this->view->artworks = $artworks;
		
    }

    
    
    public function sendCommentEmail($message){
    	$salesEmail = $this->config["sales_email"];
    	$emailTitle = "Comment from " . $message['name'];
    	$emailContent = $message["content"];
		$mail = new Zend_Mail();
		$mail->setBodyHtml($emailContent)
			->setSubject($emailTitle)
			->setFrom($message["email"], $message['name'])
			->addTo($salesEmail)
			->send();
    }
    
    public function testEmailAction(){
    	$salesEmail = "sales@fidy.net";
    	$emailTitle = "Notice, the order for artwork xxx has been paid.";
    	$emailContent = <<<EOF
    	Dear,
    		the order for artwork xxx has been paid, please confirm, here is the link to this order:
    		<a href="http://www.fidy.net/admin/show-order/1" target="_blank">http://www.fidy.net/admin/show-order/1</a>
EOF;
		$mail = new Zend_Mail();
		$mail->setBodyHtml($emailContent)
			->setSubject($emailTitle)
			->setFrom("admin@fidy.net", "system admin")
			->addTo($salesEmail)
			->addTo("fidy.watcher@gmail.com")
			->addTo("1608688@qq.com")
			->send();
			exit();
    }
    
    
    


}





















